*************************************************************************

*Table 5: Heterogeneous effects by Aadhaar seeding
*This table reports estimated differential treatment effects by Aadhaar seeding status in endline one 

*************************************************************************

use "C:\Users\tbrai\Dropbox\github_private\ABBA\replication/data/abba_seeding.dta",clear

svyset [pw = pweight]

count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)


* Program
*************************************************************************
program mydisp, eclass
         matrix betass=e(b)
         matrix colnames betass = "one" "two" "three" "four"
         mat list betass
         ereturn repost b = betass, rename
 end
*************************************************************************

********************************************************
* generate heterogeneity vars
********************************************************

gen seeded_ind=1 if uidcount>0 & !missing(uidcount)
replace seeded_ind=0 if uidcount==0 


gen seeded_missing=missing(uidcount) 

*count number of obs that fall under each category 

 *seeded_ind
 
count if seeded_ind==1
loc N_seeded_1=r(N)
di in red "`N_seeded_1'"

count if seeded_ind==0
loc N_seeded_0=r(N)
di in red "`N_seeded_0'"

count if seed_ind1==1
loc N_seeded_1_1=r(N)
di in red "`N_seeded_1_1'"

count if seed_ind1==0
loc N_seeded_0_1=r(N)
di in red "`N_seeded_0_1'"

 *seeded_missing
 
count if seeded_missing==1
loc N_missing_1=r(N)
di in red "`N_missing_1'"

count if seeded_missing==0
loc N_missing_0=r(N)
di in red "`N_missing_0'"

loc hetVar "seeded_ind seed_ind1"

********************************************************
* generate indicator vars for whether received any value
********************************************************

*generate baseline    
gen value_total0_y0 = (value_total_y0>0)
replace value_total0_y0=. if missing(value_total_y0)


*gen endline vars
loc months "jan17 feb17 mar17"
foreach mon of local months{

replace value_total_`mon' = 0 if ghost_final == 1

gen value_total0_`mon' = (value_total_`mon' > 0)
replace value_total0_`mon' = . if missing(value_total_`mon')


}



*Outcomes that we have monthly measurement of & want to report unpooled
loc Moutcomes "c_total_access_cost_adj_mar17 c_access_cost_mar17 c_access_cost_adj_feb17 c_access_cost_adj_jan17"
loc BLoutcomesM "c_cost_to_access_y0 c_cost_to_access_y0 c_cost_to_access_y0 c_cost_to_access_y0"

*Pooled outcome with BL value
*Analysis sample includes ghost HH
loc ELoutcomesBLg "value_total  value_total0"
loc BLoutcomesg "value_total_y0  value_total0_y0"


*Pooled outcome with BL value
loc ELoutcomesBL "access_cost_adj"
loc BLoutcomes "c_cost_to_access_y0"


*Outcome that we only have monthly measure monthly measurement & no BL lag
loc ELoutcomes "c6_WTA"

*******************************************************************************
*Drop ghost HHs - only use this in receipts analysis
*******************************************************************************
preserve

drop if ghost_final==1
keep if ss_code == "SS01"
* JY 20180830 Move "preserve" above keep if ss_code == "SS01", otherwise the dataset will not have ghost HH
* preserve 
tempfile HHdata_surveyedEL1
save `HHdata_surveyedEL1'
restore


*******************************************************************************
* Keep if surveyed or classified as ghost
keep if ss_code == "SS01" | ghost_final == 1

preserve
tempfile HHdata_withghosts
save `HHdata_withghosts'
restore



*******************************************************************************



******************************************************
loc h = 0    //h is for counting dim for heterogeneity


foreach Hvar of local hetVar{
loc h = `h'+ 1
loc i = 0   //i is for counting which BL var for the corresponding EL1 var
loc o = 0   //o is for counting which EL1 var

use `HHdata_surveyedEL1',clear


gen TX`Hvar' = treatment*`Hvar'


foreach Ovar of local Moutcomes{    
loc o = `o'+1

loc i = `i' + 1
local lag: word `i' of `BLoutcomesM'
unab blvarname: `lag'
gen bl_var = `blvarname'
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

* Run analysis and store control mean

eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment
sum `Ovar' if treatment == 0 & `Hvar' == 0
mat n_M`h'`o'0 = r(mean)   
mat bet_0=e(b)            
mat bet_0[1,1]=n_M`h'`o'0[1,1]                 
estadd matrix qvalue = bet_0 
estadd scalar n_M = `=n_M`h'`o'0[1,1]'
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
sum `Ovar' if treatment == 0 & `Hvar' == 1
mat n_M`h'`o'1 = r(mean) 
mat bet_1=e(b)         
mat bet_1[1,1]=n_M`h'`o'1[1,1]                 
estadd matrix qvalue = bet_1 
estadd scalar n_M = `=n_M`h'`o'1[1,1]'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar' 

drop bl_var*
}


*Outcomes for 3 months pooled with BL (samples that include ghost HH)
*Analysis sample that uses ghost HH
use `HHdata_withghosts',clear

gen TX`Hvar' = treatment*`Hvar'

loc i = 0
foreach Ovar of local ELoutcomesBLg{     
loc i = `i' + 1
local lag: word `i' of `BLoutcomesg'
unab blvarname: `lag'
gen bl_var = `blvarname'
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1




loc o = `o'+1

preserve
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  TX`Hvar' `Hvar' strata pweight block_code uid bl_var bl_var_mi
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3

reshape long `Ovar', i(uid) j(month)

* Run analysis and store control mean

eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment
sum `Ovar' if treatment == 0 & `Hvar' == 0
mat n_M`h'`o'0 = r(mean)  
mat bet_0=e(b)           
mat bet_0[1,1]=n_M`h'`o'0[1,1]                 
estadd matrix qvalue = bet_0 
estadd scalar n_M = `=n_M`h'`o'0[1,1]'
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
sum `Ovar' if treatment == 0 & `Hvar' == 1
mat n_M`h'`o'1 = r(mean) 
mat bet_1=e(b)                  
mat bet_1[1,1]=n_M`h'`o'1[1,1]                 
estadd matrix qvalue = bet_1 
estadd scalar n_M = `=n_M`h'`o'1[1,1]'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

restore

drop bl_var*
}


use `HHdata_surveyedEL1',clear


gen TX`Hvar' = treatment*`Hvar'



loc i = 0
foreach Ovar of local ELoutcomesBL{
loc i = `i' + 1
local lag: word `i' of `BLoutcomes'
unab blvarname: `lag'
gen bl_var = `blvarname'
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

loc o = `o'+1

preserve
keep c_total_`Ovar'_mar17 c_`Ovar'_feb17 c_`Ovar'_jan17 treatment  TX`Hvar' `Hvar' strata pweight block_code uid bl_var bl_var_mi
rename c_`Ovar'_jan17 `Ovar'1
rename c_`Ovar'_feb17 `Ovar'2
rename c_total_`Ovar'_mar17 `Ovar'3

reshape long `Ovar', i(uid) j(month)

* Run analysis and store control mean

eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment
sum `Ovar' if treatment == 0 & `Hvar' == 0
mat n_M`h'`o'0 = r(mean)  
mat bet_0=e(b)          
mat bet_0[1,1]=n_M`h'`o'0[1,1]                 
estadd matrix qvalue = bet_0 
estadd scalar n_M = `=n_M`h'`o'0[1,1]'
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
sum `Ovar' if treatment == 0 & `Hvar' == 1
mat n_M`h'`o'1 = r(mean)
mat bet_1=e(b)                   
mat bet_1[1,1]=n_M`h'`o'1[1,1]                 
estadd matrix qvalue = bet_1 
estadd scalar n_M = `=n_M`h'`o'1[1,1]'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

restore

drop bl_var*
}

foreach Ovar of local ELoutcomes{     //o is also for EL outcomes without BL 

loc o = `o'+1
preserve
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  TX`Hvar' `Hvar' strata pweight block_code uid
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3
reshape long `Ovar', i(uid) j(month)


eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment	
sum `Ovar' if treatment == 0 & `Hvar' == 0
mat n_M`h'`o'0 = r(mean)                 
mat bet_0=e(b)
mat bet_0[1,1]=n_M`h'`o'0[1,1]                 
estadd matrix qvalue = bet_0 
estadd scalar n_M = `=n_M`h'`o'0[1,1]'
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
sum `Ovar' if treatment == 0 & `Hvar' == 1
mat bet_1=e(b)
mat n_M`h'`o'1 = r(mean)                  
mat bet_1[1,1]=n_M`h'`o'1[1,1]                 
estadd matrix qvalue = bet_1 
estadd scalar n_M = `=n_M`h'`o'1[1,1]'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

restore


}

*No longer reporting estimates for all access cost measure
eststo het`h'0: appendmodels  het`h'50 het`h'60 het`h'80 het`h'10 

}

* prepare for number of obs for each category
loc str_ind_yes = "\emph{\textit{(N=`N_seeded_1')}}"
loc str_ind_no = "\emph{\textit{(N=`N_seeded_0')}}"
loc str_miss_yes = "\emph{\textit{(N=`N_missing_1')}}"
loc str_miss_no = "\emph{\textit{(N=`N_missing_0')}}"

loc str_ind_yes1 = "\emph{\textit{(N=`N_seeded_1_1')}}"
loc str_ind_no1 = "\emph{\textit{(N=`N_seeded_0_1')}}"

di in red "`str_ind_yes' `str_ind_no'  `str_miss_yes'  `str_miss_no' "


* Output table
loc tabname "${OutputDir}/Table5.tex" 
cd "${adoDir}"
loc nc 6


MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///	
			colnames("\specialCellCenter{No \\ `str_ind_no'}" "\specialCellCenter{Yes \\ `str_ind_yes'}"  "$\Delta$ " /// 
						"\specialCellCenter{No \\ `str_ind_no1'}" "\specialCellCenter{Yes \\ `str_ind_yes1'}"  "$\Delta$ ") width("\hsize")  ///
			mgroups("At\,least\,one\,Aadhaar\,seeded?\,(BL)" "At\,least\,one\,Aadhaar\,seeded?\,(Oct\,2016)")  ///
			pattern(1 0 0 1 0 0)

				
				
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
				
			estimates restore het150
			estimates restore het151
			estimates restore het15d
			estimates restore het250
			estimates restore het251
			estimates restore het25d

MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			addlinespace("Yes")
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("het150" "het151" "het15d" "het250" "het251" "het25d") ///
				stats() cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par($\{$ $\}$)fmt(%12.2g)) ) starlevels(* .10 ** .05 *** .01)  ///
				varlabels((1) "Value received (market prices)") 
					
eststo cr 

			estimates restore het160
			estimates restore het161
			estimates restore het16d
			estimates restore het260
			estimates restore het261
			estimates restore het26d
			
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			addlinespace("Yes")
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("het160" "het161" "het16d" "het260" "het261" "het26d") ///
				stats() cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par($\{$ $\}$)fmt(%12.2g)))   starlevels(* .10 ** .05 *** .01)  ///
				varlabels((1) "Value received\(>\)0")
					
eststo cr 

			estimates restore het180
			estimates restore het181
			estimates restore het18d
			estimates restore het280
			estimates restore het281
			estimates restore het28d
			
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			addlinespace("Yes")
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("het180" "het181" "het18d" "het280" "het281" "het28d") ///
				stats() cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par($\{$ $\}$)fmt(%12.2g)) ) starlevels(* .10 ** .05 *** .01)  ///
				varlabels((1) "Value received (WTA)")
					
eststo cr 

			estimates restore het110
			estimates restore het111
			estimates restore het11d
			estimates restore het210
			estimates restore het211
			estimates restore het21d
			
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			addlinespace("Yes")

MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("het110" "het111" "het11d" "het210" "het211" "het21d") ///
				stats() cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par($\{$ $\}$)fmt(%12.2g)) ) starlevels(* .10 ** .05 *** .01)  ///
				varlabels((1) "Transaction costs") ///
				substitute("Observations" "\midrule Observations") 
				
MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 


program drop mydisp
